package com.moyuaninfo.service;

import cn.hutool.core.util.StrUtil;
import com.moyuaninfo.entity.MyyEquipmentWater;
import com.moyuaninfo.entity.MyyWaterQualityData;
import com.moyuaninfo.factory.QueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import com.moyuaninfo.repository.MyyEquipmentWaterRepository;
import com.moyuaninfo.utils.SchedulingUtils;

import java.util.Date;
import java.util.List;

/**
 * @author 邱祥霸
 * @date 2020-04-08
 */
@Component
public class MyyEquipmentWaterManger {

    @Autowired
    private MyyEquipmentWaterRepository myyEquipmentWaterRepository;

    @Autowired
    private MyyWaterQualityDataManger myyWaterQualityDataManger;

    @Autowired
    private MyyDataOffChartsManger myyDataOffChartsManger;

    @Autowired
    private SchedulingUtils schedulingUtils;

    /**
     * 添加水质设备
     *
     * @param myyEquipmentWater: 水质设备
     * @return com.example.jpa.jap_demo.entity.MyyEquipmentWater
     *       
     * @author 邱祥霸
     * @date 
     */
    public MyyEquipmentWater save(MyyEquipmentWater myyEquipmentWater) {
        return myyEquipmentWaterRepository.save(myyEquipmentWater);
    }

    /**
     * 获取水质设备对象
     *
     * @param extend1: 扩展字段1
     * @return com.example.jpa.jap_demo.entity.MyyEquipmentWater
     *       
     * @author 邱祥霸
     * @date 2020-04-08
     */
    public MyyEquipmentWater findMyyEquipmentWater(String extend1) {
        List<MyyEquipmentWater> list = myyEquipmentWaterRepository.findAll(QueryFactory.equal("extend1", extend1));
        return CollectionUtils.isEmpty(list)? null : list.get(0);
    }

    /**
     * 保存RTU水质数据
     *
     * @param iccid: SIM 卡 ICCID
     * @param type: 水质数据类型
     * @param unit: 水质数据单位
     * @param value: 水质数据
     * @return org.springframework.data.domain.Page<com.example.jpa.jap_demo.entity.MyyEquipmentWater>
     *
     * @author 邱祥霸
     * @date 2020/4/8
     */
    public boolean saveRtuMessage(String iccid, String type, String unit, String value) {
        // 没有iccid
        if (StrUtil.isBlank(iccid)) {return false;}

        // 获取水质设备
        MyyEquipmentWater myyEquipmentWater = this.findMyyEquipmentWater(iccid);

        // 没有找到水质设备
        if (null == myyEquipmentWater) {return false;}

        // 获取最新水质数据
        MyyWaterQualityData myyWaterQualityDataNew = myyWaterQualityDataManger.findMyyWaterQualityDataNew(myyEquipmentWater.getEquipmentWaterId());

        // 获取下一次存储数据的时间节点
        Date date = schedulingUtils.getNextExecDate(new Date(myyWaterQualityDataNew.getCreateTime()));
        if (null == date) {return false;}

        // 不满足存储数据规则
        Date currentDate = new Date();
        if (currentDate.before(date)) {return false;}

        // 保存水质数据
        MyyWaterQualityData myyWaterQualityData = MyyWaterQualityData.builder()
                .equipmentWaterId(myyEquipmentWater.getEquipmentWaterId())
                .createTime(currentDate.getTime())
                .updateTime(currentDate.getTime())
                .deviceGroupAddressId(myyEquipmentWater.getDeviceGroupAddressId())
                .riverSection(myyEquipmentWater.getDeviceAddr())
                .riverwayId(myyEquipmentWater.getRiverwayId())
                .type(type)
                .unit(unit)
                .value(value)
                .build();
        myyWaterQualityDataManger.save(myyWaterQualityData);

        // 保存水质超标数据
        myyDataOffChartsManger.save(myyWaterQualityData);
        return true;
    }
}
